package com.Util;

import com.Bean.IP;
import net.sf.json.JSONObject;

import java.sql.*;
import java.util.HashSet;
import java.util.Set;

public class IPDao extends DB {
    public static Set<IP> selectIP_CanUse(){
        Set<IP> ipSet= new HashSet<IP>();
        connector();
        try (Connection conn = DriverManager.getConnection(URL, USER, PWD)) {
            if (DB.isDebug()) {
                System.out.println("IPDao.getIP() processing");
            }
            Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);

            ResultSet rs = stmt.executeQuery("select * from "+DBIP+" where status= 0 order by posttime DESC ");

            int i=0;
            while(rs.next()) {
                IP ip = new IP();
                ip.setUsername(rs.getString("username"));
                ip.setIp(rs.getString("id"));
                ip.setStatus(rs.getString("status"));
                ipSet.add(ip);
                if (DB.isDebug()) {

                    JSONObject jb = JSONObject.fromObject(ip);
                    String tojson = jb.toString();
                    System.out.println("ip["+i+"] for select");
                    System.out.println(tojson);
                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ipSet;
    }

    /**
     * 传入一个IP对象，将其数据插入到数据库中
     * @param ip
     * @return 运行状态值,1表示成功, 0表示失败
     */
    public static int updateIP(IP ip){
        connector();
        try (Connection conn = DriverManager.getConnection(URL, USER, PWD)) {
            PreparedStatement stat = conn.prepareStatement("insert into `"+DBIP+"` (`username`,`ip`,`status`)" +
                    "values(?,?,?)");
            if (DB.isDebug()) {
                System.out.println("IPDao.updateIP() processing");
                JSONObject jb = JSONObject.fromObject(ip);
                String tojson = jb.toString();
                System.out.println("ip for update");
                System.out.println(tojson);
            }

            stat.setString(1, ip.getUsername());
            stat.setInt(2, Integer.parseInt(ip.getIp()));
            stat.setInt(3, Integer.parseInt(ip.getStatus()));
            if (DB.isDebug()) {
                System.out.println("Complete stat.setString()");
                System.out.println(stat);
            }
            int result =  stat.executeUpdate();
            return result;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 传入ip,根据ip把其姓名和状态改变
     * @param ip
     */
    public static void modifySatus(IP ip){
        connector();
        try (Connection conn = DriverManager.getConnection(URL, USER, PWD)) {
            PreparedStatement stat = conn.prepareStatement("update `"+DBIP+"` set `status`=1, `username`=? where `ip`=?");
            stat.setString(1, ip.getIp());
            stat.setString(2, "'" + ip.getUsername() + "'");
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
